﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FastReflectionLib;
using System.Reflection;
using SZHL.Data;
using Newtonsoft.Json;
using SZHL.UI.AppCode;
using System.Data;

namespace SZHL.UI.API
{
    public class JFBXManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(JFBXManage).GetMethod(msg.Action.ToUpper());
            JFBXManage model = new JFBXManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 添加经费报销
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">经费报销实体json</param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void ADDJFBX(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_JFBX jfbx = JsonConvert.DeserializeObject<ZH_JFBX>(P1);

            if (jfbx.ID == 0)
            {
                jfbx.ShenQingRen = string.IsNullOrEmpty(jfbx.ShenQingRen) ? UserInfo.User.UserRealName : jfbx.ShenQingRen;
                jfbx.CRDate = DateTime.Now;
                jfbx.CRUser = UserInfo.User.UserName;
                jfbx.SPStatus = "0";
                jfbx.SPStatus1 = "0";
                jfbx.BranchName = new JH_Auth_BranchB().GetBranchNameByCode(UserInfo.User.BranchCode);
                jfbx.BranchNo = UserInfo.User.BranchCode;
                jfbx.intProcessStanceid = new Yan_WF_PDB().GetProcessID("经费报销");
                jfbx.FormCode = new ZH_JFBXB().GetFormCode("ZH_JFBX");
                new ZH_JFBXB().Insert(jfbx);
                string strSQL = string.Format("update ZH_JFBX_Item set status=1 where  Id in ({0})", jfbx.ItemIds);
                new ZH_JFBXB().ExsSql(strSQL);
            }
            else
            {
                new ZH_JFBXB().Update(jfbx);
            }
            msg.Result = jfbx;
        }
        /// <summary>
        /// 添加报销经费详细
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDJFBXITEM(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {

            ZH_JFBX_Item item = JsonConvert.DeserializeObject<ZH_JFBX_Item>(P1);

            if (item.ID == 0)
            {
                item.CRDate = DateTime.Now;
                item.CRUser = UserInfo.User.UserName;
                item.status = 0;
                item.ShenQingRen = UserInfo.User.UserRealName;
                new ZH_JFBX_ItemB().Insert(item);
            }
            else
            {
                new ZH_JFBX_ItemB().Update(item);
            }
            msg.Result = item;
        }
        /// <summary>
        /// 获取要添加经费报销列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETJFBXITEMBYIDS(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            List<ZH_JFBX_Item> list = null;
            if (P1 == "")
            { 
                 list = (List<ZH_JFBX_Item>)new ZH_JFBX_ItemB().GetEntities(d => d.status == 0 && d.CRUser == UserInfo.User.UserName);
            }
            else
            {
                string[] Ids = P1.Split(',');
                list = (List<ZH_JFBX_Item>)new ZH_JFBX_ItemB().GetEntities(d => Ids.Contains(d.ID.ToString()) && d.status == 0);

            }
            msg.Result = list;
            msg.Result1 = list.Sum(d => d.BXJE);
            string itemIds = "";
            foreach (ZH_JFBX_Item item in list)
            {
                itemIds += item.ID + ",";
            }
            itemIds = itemIds.Substring(0, itemIds.Length - 1);
            msg.Result2 = itemIds;
        }
        /// <summary>
        /// 获取经费报销内容列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETJFBXITEMLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new ZH_JFBX_ItemB().GetEntities(d => d.status == 0 && d.CRUser == UserInfo.User.UserName);
        }
        /// <summary>
        /// 获取经费报销列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">报销内容</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETUSERJFBXLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = string.Format("SELECT ID,BXZJE,ShenQingRen,SPStatus,SPStatus1,FormCode,BXDate,BXContent  from ZH_JFBX where CRUser='{0}' ", UserInfo.User.UserName);
            if (P1 != "")
            {
                strSql += string.Format("and BXContent like '%{0}%'", P1);
            }
            msg.Result = new ZH_JFBXB().GetDTByCommand(strSql);
            msg.Result1 = new Yan_WF_TDB().GetTaskStatus(2);
        }
        /// <summary>
        /// 获取经费报销列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">报销内容</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETJFBXLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = string.Format("SELECT ID,BXZJE,ShenQingRen,SPStatus,SPStatus1,FormCode,BXContent,BranchName,BXDate,BXContent  from ZH_JFBX ");
            string strWhere = " Where 1=1 ";
            if (P1 != "")
            {
                strWhere += string.Format(" And BXContent like '%{0}%'", P1);
            }

            msg.Result = new ZH_JFBXB().GetDTByCommand(strSql + strWhere + " order by crdate desc");
            msg.Result1 = new Yan_WF_TDB().GetTaskStatus(2);
        }
        /// <summary>
        /// 删除经费报销单
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">Id</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELJFBXBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                ZH_JFBX jfbx = new ZH_JFBXB().GetEntity(d => d.ID.ToString() == P1);
                if (new ZH_JFBXB().Delete(d => d.ID.ToString() == P1))
                {
                    new ZH_JFBX_ItemB().GetEntities(d => jfbx.ItemIds.Split(',').Contains(d.ID.ToString()));
                    msg.ErrorMsg = "";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取经费报销信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">Id</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETJFBXMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_JFBX jfbx = new ZH_JFBXB().GetEntity(d => d.ID.ToString() == P1);
            msg.Result = jfbx;
            msg.Result1 = new ZH_JFBX_ItemB().GetEntities(d => jfbx.ItemIds.Split(',').Contains(d.ID.ToString()));
            msg.Result2 = new Yan_WF_TDB().GetTaskStatus(2);
        }
        /// <summary>
        /// 获取经费报销详细
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">Id</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELJFBXITEM(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                if (new ZH_JFBX_ItemB().Delete(d => P1.Split(',').Contains(d.ID.ToString())))
                {
                    msg.ErrorMsg = "";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 审批列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETSPJFBXLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " ";
            if (P1 != "")
            {
                strWhere += string.Format(" And ccxj.BXContent like '%{0}%'", P1);
            }

            DataTable dt = GetSPList("ZH_JFBX", "ccxj.ID,BXZJE,ShenQingRen,FormCode,BXContent,BXDate,BXContent", strWhere, UserInfo.User.UserName);
            msg.Result = dt;
            msg.Result1 = new Yan_WF_TDB().GetTaskStatus(1);
        }

        public DataTable GetSPList(string tableName, string colName, string strWhere, string userName)
        {
            //获取第一步的审批列表
            string strSQL = string.Format(@"select   DISTINCT  {0},SPStatus
                            from  {1}  ccxj inner join Yan_WF_TD   td on ccxj.intProcessStanceid=td.ProcessDefinitionID
                            inner join JH_Auth_UserRole ur on ur.RoleCode=td.AssignedRole", colName, tableName);
            if (new Yan_WF_TDB().GetTaskIsDetail(1) == "Y")
            {
                strSQL += @" inner join JH_Auth_User u on ur.UserName=u.UserName and ccxj.BranchNo=u.BranchCode";
            }
            strSQL += string.Format(" where ur.UserName ='{0}'  and td.Taskorder=2 {1}", userName, strWhere);
            //获取第二步的审批列表
            string strSQL2 = string.Format(@"select   DISTINCT  {0},SPStatus1
                            from {1}  ccxj inner join dbo.Yan_WF_TD   td on ccxj.intProcessStanceid=td.ProcessDefinitionID
                            inner join JH_Auth_UserRole ur on ur.RoleCode=td.AssignedRole", colName, tableName);

            strSQL2 += string.Format(" where ur.UserName ='{0}' and td.Taskorder=3 and ccxj.SPStatus=1 {1}", userName, strWhere);
            DataTable dt = new Yan_WF_TDB().GetDTByCommand(strSQL + " UNION all " + strSQL2);
            if (dt.Rows.Count > 1)
            {
                foreach (DataRow row in dt.Rows)
                {
                    DataRow[] rows = dt.Select(" ID=" + row["ID"]);
                    if (rows.Length > 1 && rows[0]["SPStatus"] != rows[1]["SPStatus"] && row["SPStatus"].ToString() == "0")
                    {
                        row.Delete();
                    }
                    else if (rows.Length > 1 && rows[0]["SPStatus"] == rows[0]["SPStatus"])
                    {
                        row.Delete();
                    }
                }
                dt.AcceptChanges();
            }
            return dt;
        }
        /// <summary>
        /// 审批经费报销
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">经费报销Id</param>
        /// <param name="P2">意见</param>
        /// <param name="UserInfo"></param>
        public void SPJFBX(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_JFBX jfbx = new ZH_JFBXB().GetEntity(d => d.ID.ToString() == P1);
            string status = context.Request["spstatus"]; //状态

            if (jfbx.SPStatus != "0")
            {
                jfbx.SPStatus1 = status;
                jfbx.SPYJ1 = P2;
                jfbx.SPTime1 = DateTime.Now;
                jfbx.SPUser1 = UserInfo.User.UserName;
            }
            else
            {
                jfbx.SPStatus = status;
                jfbx.SPYJ = P2;
                jfbx.SPTime = DateTime.Now;
                jfbx.SPUser = UserInfo.User.UserName;
            }
            new ZH_JFBXB().Update(jfbx);
            msg.Result = jfbx;
        }
    }
}